What types a script's parameters can be, how a container argument really behaves once it is inside the function, and what a script can hand back.
A script's own parameters get their type the same way any function's do. You can write the type straight after the parameter's name, like ordinary code, or in a /// @arg comment above the function - both work, and neither is going away:
// the type written after the name
function heal(amount: int, log: list<string>): real { ... }
// the same script with tags
/// @arg {int} amount
/// @arg {list<string>} log
/// @returns {real}
function heal(amount, log) { ... }You can type some parameters and leave the others; a parameter you did not type is worked out from the calls, as described next. See Writing a type out for the full table, and Variables, types and scope for the tags themselves. What is worth calling out here is the fallback GBML uses when a parameter has no type written at all: it looks at every call to that script anywhere in the project, and whatever each call hands over at that position tells it the type. It does not have to be a number or a word typed straight into the call - anything GBML can already tell the type of works:
oscillate_text("MADE WITH GBABEL", room_width() * 0.5, finalSpot, lean_val, lean_type);
// text: a string x: a real (an API call times a decimal) y: an int (an instance variable)
// lean_value, lean_type: ints (instance variables)
var loc = 9; show_number(loc); // a local variable
aim(x, y); // the object's own x and y
show_number(get_n()); // what another script gives backA parameter can also be handed on from one script to another (outer(a) calling inner(a)); the type travels the whole way, however many scripts it passes through.
When the calls do not all pass the same kind of value, GBML tries to find one type that fits them all. A whole number and a decimal fit a decimal (1 and 2.5 make a real), and a reference to a child object and one to its parent fit the parent. Anything else it will not guess at: a whole number at one call and text at another is refused, and the message names both calls so you can see them.
What you write always wins. A parameter with a type written on it (amount: real or /// @arg {real} amount) keeps that type whatever the calls pass, which is how you make it wider than what the calls happen to send today. Calls are still checked against it.
GBML stops and asks for the type only when it genuinely cannot work it out - and it says why:
The type of parameter `amount` in `heal` cannot be worked out, because nothing calls `heal` with a value for it.
`heal` is given two kinds of value for `amount`: an int by the call in objects/obj_a/step.gbml on line 4, and a string by the call in objects/obj_b/step.gbml on line 9. One parameter cannot hold both.
The type of parameter `a` in `ping` cannot be worked out, because `ping` and `pong` are waiting for each other: the type of each one depends on the other.The last one is a circle: ping takes its type from what pong gives it, and pong takes its type from ping, so neither can go first. Write the type of one of them and the circle is broken. The same happens when a call hands over a value GBML cannot tell the type of (a variable nothing ever gives a value to, or the result of a script that returns nothing): the message names the call and what it passes.
A parameter can also have a default, used whenever a call leaves it out - see Functions for the syntax; it applies to a script's own parameters exactly the same way.
Every call to a script is checked against its real parameter list, whether the types came from a tag or from agreement across every call site:
`heal` takes 2 arguments, but this call has 1.
Argument 2 to `heal` should be a real, but this is a string.int, real, bool, color, ref<obj>, and any of your project's own asset ids (sprite, sound, song, font, tileset, room, path, effect) all arrive exactly the way you would expect from the type itself - there is nothing script-specific to learn about any of them.
A string parameter accepts a variable, a literal, or a $"..." template string interchangeably, but what actually arrives is a lightweight view onto the caller's own text, not a fresh copy the script owns. This makes no practical difference for reading it - every string function still works
builds and returns a brand new string instead), so this is not a restriction you can run into by accident the way the container behavior below is.
This is the one place a script parameter behaves differently from a plain local variable, and it is worth understanding clearly. An array<T>, list<T>, grid<T>, stack<T>, queue<T>, map<K,V> or set<T> parameter does not receive a copy of whatever container the caller passed - it receives a live reference to that exact container, capacity and all:
// scripts/add_item.gbml
/// @arg {list<string>} inventory
/// @arg {string} item
function add_item(inventory, item) {
list_add(inventory, item); // really changes the CALLER's own list, not a private copy
}Two things follow from this, and both are deliberate:
add_item above does not care whether the caller's inventory was declared /// @capacity 8 or /// @capacity 40 - the parameter itself never names a capacity at all, so nothing about the script has to change to match a bigger or smaller list somewhere else in the project.const/read-only parameter syntax, so every container parameter is capable of writing back to the caller. Whether it actually does is up to the script's own body - list_add/list_remove/list_clear (or a plain items[i] = ...) on a parameter really does change what the caller sees after the call returns, exactly as if the caller had written those lines itself.grid<T> is the one exception worth its own warning. A grid parameter arrives the same reference-shaped way as every other container above, but a grid's own width is not something that reference has anywhere to carry - the two-number index [# x, y] needs to know it, and a parameter simply does not have it:
/// @arg {grid<int>} board
function clear_centre(board) {
board[# 1, 1] = 0; // ERROR: board is a grid<T>, but its width is not known here
}The compiler's own fix for this is exactly what it sounds like: index the grid in the caller, where its real width is known, and pass the plain value in or out instead of trying to index the parameter inside the function. See Grids for [# x, y] indexing in full.
A parameter never declares its own size either way - there is no number on a parameter for a /// @capacity tag to attach to, and writing one in the type (log: list<string, 8>) is refused. Whatever size the caller's real container happens to have is simply what the script sees, every time.
Structs and constructors already covers the general struct-parameter gap: a /// @arg {struct Name} parameter compiles, but reading one of its fields inside the function does not check yet. Worth adding here: that same gap applies to a struct arriving as a container's element too (list<struct Cell>, say), not only to a bare struct parameter - and either way, "Name" has to match a real struct literal tagged with that exact name somewhere else in the project, or the parameter's own signature has nothing to build against at all. A script that only ever passes its struct-shaped parameter along, without reading a field off it, is unaffected by either limit.
A script's return type comes from an explicit type - function greeting(name: string): string or a /// @returns {type} tag, whichever you prefer - or from what its own return statements actually return, with nothing written at all:
// scripts/greeting.gbml
/// @arg {string} name
function greeting(name) {
return $"Hello, {name}!"; // return type inferred as string, no /// @returns needed
}A script with no return at all returns nothing. Unlike a parameter, a returned container or string comes back by value - a real, independent copy the caller now owns, never a reference into anything internal to the script. This is the mirror image of the parameter story above: a container goes in as a live reference so the script can act on the caller's own data; a container comes out as its own separate value the caller is free to keep or discard.